﻿-- -----------------------------------------------------------------------------
-- | WonderTek [ 网络无处不在，沟通及时到达 ]
-- -----------------------------------------------------------------------------
-- | Copyright (c) 2012, WonderTek, Inc. All Rights Reserved.
-- -----------------------------------------------------------------------------
-- | Author: yeyu <yeyu@mantis.com>
-- -----------------------------------------------------------------------------
-- @class MsgPush
-- @note 消息推送
-- -----------------------------------------------------------------------------

MsgPush = {}

Reg.g_MsgPush = 'g_MsgPush'

--[[
在index.wdml页面中做一下操作

if 1 == MsgPush:bindHostIp('172.12.1.1') and 1 == MsgPush:setListener(Sprite:getCurScene(), 1001) then
    if 0 == MsgPush:getServiceStatus() then
        MsgPush:setServiceStatus(1)
    end
    -- MsgPush:setPreferences(style, status)
end

function bodyOnPluginEvent(msg, param)
    if msg == 1001 then
        local title = Param:getString(param, 0)
        local msg = Param:getString(param, 1)
        local uri = Param:getString(param, 2)
        local reg = Reg:create(Reg.com_wondertek_xxx.msgpush)
        Reg:setString(reg, 'title', title)
        Reg:setString(reg, 'message', msg)
        Reg:setString(reg, 'uri', uri)
        Scene:setReturn(Alias.index, Alias.msgpush)
        Scene:go(Alias.msgpush)
    end
end
--]]


--[[
 -------------------------------------------------------------------------------
 -- @function MsgPush:_getHandle()
 -------------------------------------------------------------------------------
 -- @brief 获取MsgPush插件句柄
 -------------------------------------------------------------------------------
 -- @access private
 -------------------------------------------------------------------------------
 -- @param 无
 -------------------------------------------------------------------------------
 -- @return number
 -------------------------------------------------------------------------------
 --]]
function MsgPush:_getHandle()
    local reg = registerCreate(Reg.g_MsgPush)
    local handle = registerGetInteger(reg, 'MsgPush')
    if handle == 0 then
        handle = pluginCreate('MsgPush')
        registerSetInteger(reg, 'MsgPush', handle)
    end
    return handle
end

--[[
  ------------------------------------------------------------------------------
  --@function MsgPush:bindHostIp(ip)
  --------------------------------------------------------------------------------
  --@breif 绑定MsgPush后台服务监听的推送服务器ip。后台服务打开前必须设定
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param ip: string, 消息服务器ip地址，(不设定默认为:"127.0.0.1")
  ------------------------------------------------------------------------------
  --@sample 参考用法如下：local result = MsgPush:bindHostIp(ip)
  ------------------------------------------------------------------------------
  --@return number, 1表示启动成功，0代表失败
  ------------------------------------------------------------------------------
  --]]
function MsgPush:bindHostIp(ip)
    return pluginInvoke(self:_getHandle(), 'BindHostIp', ip)
end

--[[
  ------------------------------------------------------------------------------
  --@function MsgPush:setListener(observer, msg)
  --------------------------------------------------------------------------------
  --@breif 设置应用监听页面(只需一个监听者,多次调用此接口,以最后一次调用为准)。
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param observer: number, 哪个场景监听该事件
  --@param msg: number, 消息号，在OnPluginEvent事件处理函数中使用
  ------------------------------------------------------------------------------
  --@sample 参考用法如下：local result = MsgPush:setListener(observer, msg)
  ------------------------------------------------------------------------------
  --@return number, 1表示启动成功，0代表失败
  ------------------------------------------------------------------------------
  --]]
function MsgPush:setListener(observer, msg)
    return pluginInvoke(self:_getHandle(), 'CreateListener', observer, msg)
end

--[[
  ------------------------------------------------------------------------------
  --@function MsgPush:setServiceStatus(status)
  --------------------------------------------------------------------------------
  --@breif 设置MsgPush后台服务是否打开
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param status: number, 1代表打开, 0代表关闭
  ------------------------------------------------------------------------------
  --@sample 参考用法如下：local result = MsgPush:setServiceStatus(status)
  ------------------------------------------------------------------------------
  --@return number, 1表示启动成功，0代表失败
  ------------------------------------------------------------------------------
  --]]
function MsgPush:setServiceStatus(status)
    return pluginInvoke(self:_getHandle(), 'SetServiceStatus', status)
end

--[[
  ------------------------------------------------------------------------------
  --@function MsgPush:getServiceStatus()
  --------------------------------------------------------------------------------
  --@breif 获取MsgPush后台服务是否打开
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@sample 参考用法如下：local result = MsgPush:getServiceStatus()
  ------------------------------------------------------------------------------
  --@return number, 1表示打开，0表示关闭
  ------------------------------------------------------------------------------
  --]]
function MsgPush:getServiceStatus()
    return pluginInvoke(self:_getHandle(), 'GetServiceStatus')
end

--[[
  ------------------------------------------------------------------------------
  --@function MsgPush:setPreferences(style, status)
  --------------------------------------------------------------------------------
  --@breif 设置收到推送消息时,通知风格
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param key: string, "Notification" 是否系统通知栏显示,若此属性设为是,则其他属性才有效;"Sound" 通知栏显示时,是否同时声音提示(默认是);"Vibrate" 通知栏显示时,是否同时震动提示(默认否);"Toast" 通知栏显示时,是否同时Toast提示(默认否)
  --@param status: number, 1代表打开, 0代表关闭
  ------------------------------------------------------------------------------
  --@return number, 1表示设置成功，0代表设置失败
  ------------------------------------------------------------------------------
  --]]
function MsgPush:setPreferences(style, status)
    return pluginInvoke(self:_getHandle(), 'SetPreferences', style, status)
end

--[[
  ------------------------------------------------------------------------------
  --@function MsgPush:getPreferences(style)
  --------------------------------------------------------------------------------
  --@breif 获取通知风格
  ------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@param key: string, "Notification" 是否系统通知栏显示,若此属性设为是,则其他属性才有效;"Sound" 通知栏显示时,是否同时声音提示(默认是);"Vibrate" 通知栏显示时,是否同时震动提示(默认否);"Toast" 通知栏显示时,是否同时Toast提示(默认否)
  ------------------------------------------------------------------------------
  --@return number, 1表示设置成功，0代表设置失败
  ------------------------------------------------------------------------------
  --]]
function MsgPush:getPreferences(style)
    return pluginInvoke(self:_getHandle(), 'GetPreferences', style)
end

--[[
  ------------------------------------------------------------------------------
  --@function MsgPush:setUser(key, value)
  --------------------------------------------------------------------------------
  --@brief 设定客户端用户信息方便服务端进行管理
  --------------------------------------------------------------------------------
  --@access public
  ------------------------------------------------------------------------------
  --@sample 参考用法：local ret = pluginInvoke(plugin, "SetUser", "username","admin")
  --------------------------------------------------------------------------------
  --@params key:  string, "username", "password"
  --@params value:  string, 对应关键词的值
  --------------------------------------------------------------------------------
  --@return  ret:  number, 1表示成功，0表示失败
  --------------------------------------------------------------------------------
  --]]
function MsgPush:setUser(key, value)
    return pluginInvoke(self:_getHandle(), 'SetUser', key, value)
end

--[[
  ------------------------------------------------------------------------------
  --@function MsgPush:setApiKey(apiKey)
  --------------------------------------------------------------------------------
  --@brief 设定消息推送ApiKey，需要与服务端匹配
  --------------------------------------------------------------------------------
  --@access public
  --------------------------------------------------------------------------------
  --@params apiKey:  string, 消息推送apiKey(不设定默认为：1234567890)
  --------------------------------------------------------------------------------
  --@return  ret:  number, 1表示成功，0表示失败
  --------------------------------------------------------------------------------
  --]]
function MsgPush:setApiKey(apiKey)
    return pluginInvoke(self:_getHandle(), 'SetMsgPushApiKey', apiKey)
end